package com.stm.bi.core.util;

import com.stm.bi.model.Title;

import java.util.*;

import static com.stm.bi.constant.PlanConstants.*;
import static com.stm.bi.core.util.DateUtils.*;

public class ETLUtils {


    public static Map<Long, Long> getWarehouseDeptIdMap(Map<Long, Long> deptMap, List<Long> deptIds){

        Map<Long, Long> res = new HashMap<>();

        for(Long deptId: deptMap.keySet()){
            setWarehouseDeptIdMap(deptId, deptId, deptIds, deptMap, res);
        }

        return res;
    }

    public static void setWarehouseDeptIdMap(Long id, Long warehouseDeptId, List<Long> deptIds, Map<Long, Long> deptMap, Map<Long, Long> map){
        if(deptIds.contains(warehouseDeptId)){
            map.put(id, warehouseDeptId);
        }else {
            Long parentId = deptMap.get(warehouseDeptId);
            if(parentId == null || parentId == 0L){
                return;
            }
            setWarehouseDeptIdMap(id, parentId, deptIds, deptMap, map);
        }
    }

    public static List<String> getDateList(String startDate, String endDate, String dateType){

        List<String> l = new ArrayList<>();
        if (Objects.equals(dateType, TYPE_DAY)) {
            if(startDate.length()< 10){
                startDate = startDate.substring(0,7) + "-01";
            }
            if(endDate.length() < 10){
                endDate = endDate.substring(0,7) + "-" + getMonthDays(endDate.substring(0,7));
            }
            while (startDate.compareTo(endDate) <= 0) {
                l.add(startDate);
                startDate = getNextDay(startDate);
            }
        } else if (Objects.equals(dateType, TYPE_MONTH)) {
            if(startDate.length() < 7){
                startDate = startDate.substring(0,4) + "-01";
            }
            if(endDate.length() < 7){
                endDate = endDate.substring(0,4) + "-12";
            }
            while (startDate.compareTo(endDate) <= 0) {
                l.add(startDate);
                startDate = getNextMonth(startDate);
            }
        } else if (Objects.equals(dateType, TYPE_HOUR)) {
            while (startDate.compareTo(endDate) <= 0) {
                l.add(startDate);
                startDate = getNextHour(startDate);
            }
        } else if (Objects.equals(dateType, TYPE_WEEK)){

            if(startDate.length() < 7){
                startDate = getWeekFirstDay(getWeek(startDate.substring(0,4) + "-01-01"));
            }else if(startDate.length() < 10){
                startDate = getWeekFirstDay(getWeek(startDate + "-01"));
            }else {
                startDate = getWeekFirstDay(getWeek(startDate));
            }

            if(endDate.length() < 7){
                endDate = getWeekFirstDay(getWeek(endDate.substring(0,4) + "-12-31"));
            }else if(endDate.length() < 10){
                endDate = getWeekFirstDay(getWeek(endDate.substring(0,7) + "-" + addZero(getMonthDays(endDate.substring(0,7)))));
            }else {
                endDate = getWeekFirstDay(getWeek(endDate));
            }

            while (startDate.compareTo(endDate) <= 0){
                l.add(startDate);
                startDate = getWeekFirstDay(getNextWeek(getWeek(startDate)));
            }
        }
        return l;
    }


    public static List<Title> getTitles(List<String> dates, String dateType){
        List<Title> titles = new ArrayList<>();
        for(String date: dates){
            if(Objects.equals(dateType, TYPE_WEEK)){
                titles.add(Title.of(date, DateUtils.getWeekFullColumnByDate(date)));
            }else if(Objects.equals(dateType, TYPE_DAY)){
                titles.add(Title.of(date, date.substring(5,10)));
            }else if(Objects.equals(dateType, TYPE_MONTH)){
                titles.add(Title.of(date, DateUtils.getNameByCycle(date, TYPE_MONTH)));
            }
        }

        return titles;
    }

    public static List<Title> getTitles(String startDate, String endDate, String dateType){
        List<String> dates = getDateList(startDate, endDate, dateType);
        return getTitles(dates, dateType);
    }
}
